import 'package:acgn_pioneer/plugin/dialog/src/constants.dart';
import 'package:acgn_pioneer/utils/pop_util.dart';
import 'package:acgn_pioneer/widgets/reader/catalog.dart';
import 'package:acgn_pioneer/widgets/reader/detail.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';

import '../../public.dart';
import 'barrage/_index.dart';
import 'events/_index.dart';
import 'mixins/widget_base.dart';
import 'light.dart';

/// 底部显示章节信息
class ReaderBottomMenu extends StatefulWidget {
  ReaderBottomMenu({Key key}) : super(key: key);

  @override
  ReaderBottomMenuState createState() => new ReaderBottomMenuState();
}

class ReaderBottomMenuState extends State<ReaderBottomMenu>
    with ReaderWidgetBase, TickerProviderStateMixin {
  AnimationController controller;
  Animation animation;

  bool isShow = true; // 是否显示菜单
  var sub; // 章节图片序号改变
  var sub1; // 阅读器事件-开始执行
  var sub2; // 关闭底部菜单
  var sub3; // 章节改变

  @override
  void initState() {
    super.initState();

    sub =
        readerPage.bus.on<ReaderChapterImageIndexChangeEvent>().listen((event) {
      refreshView();
    });

    sub1 = readerPage.bus.on<ReaderRunEvent>().listen((event) {
      refreshView();
    });
    sub2 = readerPage.bus.on<ReaderCloseMenuEvent>().listen((event) {
      if (!event.isToggle && !isShow) {
        return;
      }
      // toggleAnimation();
    });
    sub3 = readerPage.bus.on<ReaderChapterIndexChangeEvent>().listen((event) {
      refreshView();
    });

    controller = AnimationController(
        duration: const Duration(milliseconds: 500), vsync: this);
    animation =
        Tween(begin: Offset.zero, end: Offset(0, 1)).animate(controller);
  }

  @override
  void dispose() {
    sub.cancel();
    sub1.cancel();
    sub2.cancel();
    sub3.cancel();

    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var current = readModel.currentChapter;

    if (current == null) {
      return Container();
    }

    return Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        child: SliderAnimation(animation: animation, child: _body(current)));
  }

  Widget _body(ChapterModel current) {
    return Container(
      padding: EdgeInsets.only(top: 20, right: 20, bottom: 10, left: 20),
      child: Column(
        children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SVG.svg('read_book', size: 16),
                    Container(
                        constraints: BoxConstraints(maxWidth: Rem(230)),
                        child: Text(
                          ' ${current.comicModel.name}',
                          style: TextStyle(color: Colors.white, fontSize: 18),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        )),
                    InkWell(
                        child: Row(
                          children: [
                            Text('    详情',
                                style: TextStyle(
                                    color: Colors.white, fontSize: 12)),
                            SVG.icon('arrow_right',
                                size: 12, color: Colors.white),
                          ],
                        ),
                        onTap: () {
                          showDetail(current);
                        })
                  ],
                ),
                Container(
                    margin: EdgeInsets.only(top: 4),
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            constraints: BoxConstraints(maxWidth: Rem(230)),
                            child: Text(
                              '当前阅读：${current.name} ${readModel.currentImageIndex}/${current.totalPage}    ',
                              // '当前阅读： ${readModel.currentImageIndex}/${current.totalPage}    ',
                              style:
                                  TextStyle(color: Colors.white, fontSize: 12),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          ReaderCatalog(
                            bus: readerPage.bus,
                            comicIds: readerMainPage.comicIds,
                            comicInfo: readModel.currentChapter.comicModel,
                            currentChpaterId: current.id,
                            textStyle:
                                TextStyle(color: Colors.white, fontSize: 12),
                          )
                        ]))
              ],
            ),
            ReaderLight()
          ]),
          SizedBox(height: 10),
          ReaderBarrageMenu()
        ],
      ),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
            begin: Alignment.bottomCenter,
            end: Alignment.topCenter,
            colors: [Color.fromARGB(200, 0, 0, 0), Color.fromARGB(0, 0, 0, 0)]),
      ),
    );
  }

  showDetail(currentChapter) {
    PopUtil.show(context, ReaderDetail(data: currentChapter, simple: true),
        alignment: Alignment.bottomCenter,
        transition: DialogTransition(
            duration: Duration(milliseconds: 400),
            type: AnimationType.fromBottom),
        padding: EdgeInsets.only(top: Rem(18)));
  }

  /// 动画执行
  toggleAnimation() {
    if (isShow) {
      controller.forward();
    } else {
      controller.reverse();
    }
    isShow = !isShow;
  }
}
